package com.rtx.edi.platform.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.edi.model.application.dto.PlApplicationDto;
import com.rtx.edi.model.application.po.PlApplication;

import com.rtx.edi.platform.application.mapper.PlApplicationMapper;
import com.rtx.edi.platform.application.service.PlApplicationService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 应用信息表  服务实现类
 * </p>
 *
 * @author abc
 * @since 2023-03-06
 */
@Service
public class PlApplicationServiceImpl extends ServiceImpl<PlApplicationMapper, PlApplication> implements PlApplicationService {

	@Autowired
	PlApplicationMapper plApplicationMapper;
	
    @Override
    public void selectPlApplicationList(Page<PlApplication> pageParam, PlApplication plApplication) {
        LambdaQueryWrapper<PlApplication> query = Wrappers.lambdaQuery();
        query.orderByAsc(PlApplication::getCompanyId,PlApplication::getApplicationNo);
        if(plApplication != null){
            //公司名称、应用编号、应用名称、应用类型、应用状态
            query
                    .eq(plApplication.getCompanyId() != null,PlApplication::getCompanyId,plApplication.getCompanyId())
                    .eq(StringUtils.hasText(plApplication.getApplicationNo()),PlApplication::getApplicationNo,plApplication.getApplicationNo())
                    .like(StringUtils.hasText(plApplication.getApplicationName()),PlApplication::getApplicationName,plApplication.getApplicationName())
                    .eq(plApplication.getApplicationType() != null,PlApplication::getApplicationType,plApplication.getApplicationType())
                    .eq(plApplication.getApplicationStatus() != null,PlApplication::getApplicationStatus,plApplication.getApplicationStatus());
        }
        baseMapper.selectPage(pageParam,query);
    }

    @Override
    public Boolean addPlApplication(PlApplication plApplication) {
        //验证非空字段
        checkField(plApplication);
        //验证应用编号是否存在
        LambdaQueryWrapper<PlApplication> query1 = Wrappers.lambdaQuery();
        query1.eq(PlApplication::getCompanyId,plApplication.getCompanyId())
                .eq(PlApplication::getApplicationNo,plApplication.getApplicationNo());
        Assert.isTrue(!baseMapper.exists(query1),"应用编号已存在");
        //验证应用名称是否存在
        LambdaQueryWrapper<PlApplication> query2 = Wrappers.lambdaQuery();
        query2.eq(PlApplication::getCompanyId,plApplication.getCompanyId())
                .eq(PlApplication::getApplicationName,plApplication.getApplicationName());
        Assert.isTrue(!baseMapper.exists(query2),"应用名称已存在");
        return baseMapper.insert(plApplication) == 1;
    }

    /**
     * 验证非空字段
     * @param plApplication
     */
    private void checkField(PlApplication plApplication){
//        Assert.notNull(plApplication.getCompanyId(),"公司id不能为空");
        Assert.hasText(plApplication.getApplicationNo(),"应用编号不能为空");
        Assert.hasText(plApplication.getApplicationName(),"应用名称不能为空");
        Assert.notNull(plApplication.getApplicationType(),"应用类型不能为空");
        Assert.notNull(plApplication.getApplicationStatus(),"应用状态不能为空");
//        Assert.hasText(plApplication.getInsUser(),"新增用户不能为空");
    }

    @Transactional
	public void deletePlApplicationByIds(List<PlApplicationDto> list) {
		for(PlApplicationDto dto:list) {
			try {
				plApplicationMapper.updisDeletedByid(dto.getId());
			} catch (Exception e) {
				throw new RuntimeException("删除id-"+dto.getId()+"-失败");
			}
		}
	}
}
